﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 工艺主表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksTechniqueService), ServiceLifetime = LifeTime.Transient)]
    public class CksTechniqueService : BaseService<CksTechnique>, ICksTechniqueService
    {
        /// <summary>
        /// 查询工艺主表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksTechniqueDto> GetList(CksTechniqueQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<CksTechnique, CksTechniqueDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksTechnique GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加工艺主表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksTechnique AddCksTechnique(CksTechnique model)
        {
            var tech =  Insertable(model).ExecuteReturnEntity();
            var client = AsSugarClient();
            //查询模板，自动生成明细
            var lists = client.Queryable<CksTechniqueTemplate>().Where(a => a.ParentId == tech.TemplateId).OrderBy(a => a.Sequence).ToList();

            var details = new List<CksTechniqueDetails>();
            foreach (var t in lists)
            {
                var item = new CksTechniqueDetails()
                {
                    TechniqueId = tech.Id,
                    Name = t.Name,
                    ItemTemplateId = t.Id,
                    Sequence = t.Sequence,
                    DataId = t.DataId,
                    IsPrimary = t.IsPrimary
                };
                details.Add(item);
            }
            client.Insertable(details).ExecuteCommand();
            return tech;
        }

        /// <summary>
        /// 修改工艺主表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksTechnique(CksTechnique model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksTechnique> QueryExp(CksTechniqueQueryDto parm)
        {
            var predicate = Expressionable.Create<CksTechnique>();
            predicate.AndIF(parm.TemplateId>0 , a=>a.TemplateId == parm.TemplateId);
            return predicate;
        }
    }
}